<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<meta name="keyword" content="">

    <link rel="shortcut icon" href="/node-mongodb-native/3.2/img/favicon.png">

    <title>Collations</title>

    <link rel="stylesheet" href="/node-mongodb-native/3.2/lib/bootstrap.css" type="text/css" />
<link rel="stylesheet" href="/node-mongodb-native/3.2/lib/font-awesome/css/font-awesome.min.css" type="text/css" />
<link rel="stylesheet" href="/node-mongodb-native/3.2/css/mongodb-docs.css" type="text/css" />
<link rel="stylesheet" href="/node-mongodb-native/3.2/css/overrides.css" type="text/css" />
<link rel="stylesheet" href="/node-mongodb-native/3.2/lib/highlight/styles/idea.css" />
<link rel="stylesheet" href="/node-mongodb-native/3.2/lib/bootstrap-toggle/bootstrap-toggle.min.css" type="text/css" />
<link rel="stylesheet" href="/node-mongodb-native/3.2/css/java.css" type="text/css" />


  </head>

  <body>
  
  <section id="container" class="">
      
      <header id="header-db" class="row" role="navigation">
  <div class="header-content">
    <div class="toggle-nav pull-left">
        <i class="fa fa-bars"></i>
        <div class="icon-reorder tooltips" data-original-title="Toggle Navigation" data-placement="bottom"></div>
    </div>
    
    <div class="logo pull-left">
      <a href="https://www.mongodb.com/">
        <img src="https://mongodb.github.io/node-mongodb-native/img/logo-mongodb-header.png", alt="MongoDB.com" />
      </a>
    </div>
    
    <div>
<div class="nav-items pull-right">
  <a href="https://university.mongodb.com" data-toggle="tooltip" data-placement="bottom" title="Free Online Classes">MongoDB University</a>
  <a href="https://www.mongodb.com/try" data-toggle="tooltip" data-placement="bottom" title="Download MongoDB">Downloads</a>
  <a href="https://www.mongodb.com/community" data-toggle="tooltip" data-placement="bottom" title="Get involved with MongoDB">Community</a>
  <a href="https://www.mongodb.com/docs" data-toggle="tooltip" data-placement="bottom" title="The MongoDB Documentation">Docs</a>
  <a href="https://www.mongodb.com/blog" data-toggle="tooltip" data-placement="bottom" title="The MongoDB Blog">Blog</a>
  <div id="search">
<form method="get" action="//www.google.com/search" target="_blank">
    <input type="text" name="searchQuery" size="20" value="" autocomplete="off" placeholder="Search docs">
    <input type="hidden" name="site" value="/node-mongodb-native/3.2">
    <input type="hidden" name="q" value="">
    <label for="searchQuery"><i class="fa fa-search fa-1"></i></label>
</form>
</div>

</div>
</div>

  </div>
</header>


      

      
<aside id="sidebar" class="sidebar">
    <div class="ssidebar nav-collapse">
      <div class="ssidebarwrapper">
        <h3>
          <a class="index-link" href="/node-mongodb-native/3.2/../">MongoDB Node.js Driver</a>
          <a class="version pull-right" href="/node-mongodb-native/3.2">3.2</a>
        </h3>

        
        <ul class="sidebar-menu">
          
          
          
          
          
          
          
          
          
          
          
          

          
          
            
            









  


          
            
            









  


          
            
            









  
    
    
      
      
        
        
        
        
        
        
        
        









  
    
    
      
      
        
        
        
        
        
        
        
        









  


        
          
        
      
    
      
      
        
        
        
        
        
        
        
        









  


        
          
        
      
    
    
      
    
  


        
          
        
      
    
      
      
        
        
        
        
        
        
        
        









  
  
  
  
  
  
  
  

  
    
    
    
  
  


        
      
    
      
      
        
      
    
      
      
    
      
      
    
      
      
    
      
      
    
      
      
    
      
      
    
      
      
    
      
      
    
    
  


          
            
            









  
    
    
      
      
    
      
      
    
      
      
    
      
      
    
      
      
    
    
  


          
            
            









  
    
    
      
      
    
    
  


          
            
            









  


          
            
            









  


          
            
            









  


          
            
            









  


          

          

          
            
            
















    <li class="toctree-l1">
    <a href="/node-mongodb-native/3.2/installation-guide/installation-guide/">
        <i class='fa fa-puzzle-piece'></i>
        Installation Guide
    </a>
  </li>


          
            
            
















    <li class="toctree-l1">
    <a href="/node-mongodb-native/3.2/quick-start/quick-start/">
        <i class='fa fa-road'></i>
        Quick Start
    </a>
  </li>


          
            
            













  




<li class="toctree-l1  current">
  <a href="/node-mongodb-native/3.2/tutorials/main/" class="">
      <i class='fa fa-thumb-tack'></i>
      <span>Tutorials</span>
      <span class="menu-arrow fa fa-angle-down"></span>
  </a>
    <ul  class="current">
        
        
        
        
















<li class="toctree-l2 ">
  <a href="/node-mongodb-native/3.2/tutorials/connect/" class="">
      <i class='fa'></i>
      <span>Connect to MongoDB</span>
      
  </a>
    <ul >
        
        
        
        
















    <li class="toctree-l3">
    <a href="/node-mongodb-native/3.2/tutorials/connect/authenticating/">
        <i class='fa'></i>
        Authentication
    </a>
  </li>


        
        
        
        
        
















    <li class="toctree-l3">
    <a href="/node-mongodb-native/3.2/tutorials/connect/ssl/">
        <i class='fa'></i>
        SSL Settings
    </a>
  </li>


        
        
    </ul>
  </li>


        
        
        
        
        













  




    <li class="toctree-l2">
    <a href="/node-mongodb-native/3.2/tutorials/collations/">
        <i class='fa'></i>
        Collations
    </a>
  </li>


        
        
        
        
        
















    <li class="toctree-l2">
    <a href="/node-mongodb-native/3.2/tutorials/collections/">
        <i class='fa'></i>
        Collections
    </a>
  </li>


        
        
        
        
        
















    <li class="toctree-l2">
    <a href="/node-mongodb-native/3.2/tutorials/create-indexes/">
        <i class='fa'></i>
        Create Indexes
    </a>
  </li>


        
        
        
        
        
















    <li class="toctree-l2">
    <a href="/node-mongodb-native/3.2/tutorials/crud/">
        <i class='fa'></i>
        CRUD Operations
    </a>
  </li>


        
        
        
        
        
















    <li class="toctree-l2">
    <a href="/node-mongodb-native/3.2/tutorials/projections/">
        <i class='fa'></i>
        Projections
    </a>
  </li>


        
        
        
        
        
















    <li class="toctree-l2">
    <a href="/node-mongodb-native/3.2/tutorials/aggregation/">
        <i class='fa'></i>
        Aggregation
    </a>
  </li>


        
        
        
        
        
















    <li class="toctree-l2">
    <a href="/node-mongodb-native/3.2/tutorials/text-search/">
        <i class='fa'></i>
        Text Search
    </a>
  </li>


        
        
        
        
        
















    <li class="toctree-l2">
    <a href="/node-mongodb-native/3.2/tutorials/geospatial-search/">
        <i class='fa'></i>
        Geospatial Search
    </a>
  </li>


        
        
        
        
        
















    <li class="toctree-l2">
    <a href="/node-mongodb-native/3.2/tutorials/commands/">
        <i class='fa'></i>
        Database Commands
    </a>
  </li>


        
        
        
        
        
















<li class="toctree-l2 ">
  <a href="/node-mongodb-native/3.2/tutorials/gridfs/" class="">
      <i class='fa'></i>
      <span>GridFS</span>
      
  </a>
    <ul >
        
        
        
        
















    <li class="toctree-l3">
    <a href="/node-mongodb-native/3.2/tutorials/gridfs/streaming/">
        <i class='fa'></i>
        GridFS API
    </a>
  </li>


        
        
        
        
        
















    <li class="toctree-l3">
    <a href="/node-mongodb-native/3.2/tutorials/gridfs/gridstore/">
        <i class='fa'></i>
        Legacy GridStore
    </a>
  </li>


        
        
    </ul>
  </li>


        
        
    </ul>
  </li>


          
            
            
















<li class="toctree-l1 ">
  <a href="/node-mongodb-native/3.2/reference/main/" class="">
      <i class='fa fa-book'></i>
      <span>Reference</span>
      <span class="menu-arrow fa fa-angle-right"></span>
  </a>
    <ul >
        
        
        
        
















<li class="toctree-l2 ">
  <a href="/node-mongodb-native/3.2/reference/connecting/" class="">
      <i class='fa'></i>
      <span>Connection Options</span>
      
  </a>
    <ul >
        
        
        
        
















    <li class="toctree-l3">
    <a href="/node-mongodb-native/3.2/reference/connecting/connection-settings/">
        <i class='fa'></i>
        Connection Settings
    </a>
  </li>


        
        
    </ul>
  </li>


        
        
        
        
        
















<li class="toctree-l2 ">
  <a href="/node-mongodb-native/3.2/reference/ecmascriptnext/" class="">
      <i class='fa'></i>
      <span>ECMAScript Next</span>
      
  </a>
    <ul >
        
        
        
        
















    <li class="toctree-l3">
    <a href="/node-mongodb-native/3.2/reference/ecmascriptnext/connecting/">
        <i class='fa'></i>
        Connecting
    </a>
  </li>


        
        
        
        
        
















    <li class="toctree-l3">
    <a href="/node-mongodb-native/3.2/reference/ecmascriptnext/crud/">
        <i class='fa'></i>
        CRUD Operations
    </a>
  </li>


        
        
    </ul>
  </li>


        
        
        
        
        
















<li class="toctree-l2 ">
  <a href="/node-mongodb-native/3.2/reference/management/" class="">
      <i class='fa'></i>
      <span>Management</span>
      
  </a>
    <ul >
        
        
        
        
















    <li class="toctree-l3">
    <a href="/node-mongodb-native/3.2/reference/management/logging/">
        <i class='fa'></i>
        Logging
    </a>
  </li>


        
        
        
        
        
















    <li class="toctree-l3">
    <a href="/node-mongodb-native/3.2/reference/management/apm/">
        <i class='fa'></i>
        APM
    </a>
  </li>


        
        
        
        
        
















    <li class="toctree-l3">
    <a href="/node-mongodb-native/3.2/reference/management/sdam-monitoring/">
        <i class='fa'></i>
        Topology Monitoring
    </a>
  </li>


        
        
    </ul>
  </li>


        
        
        
        
        
















    <li class="toctree-l2">
    <a href="/node-mongodb-native/3.2/reference/pool/">
        <i class='fa'></i>
        Pool Design
    </a>
  </li>


        
        
        
        
        
















    <li class="toctree-l2">
    <a href="/node-mongodb-native/3.2/reference/faq/">
        <i class='fa'></i>
        Frequently Asked Questions
    </a>
  </li>


        
        
    </ul>
  </li>


          
            
            
















<li class="toctree-l1 ">
  <a href="/node-mongodb-native/3.2/upgrade-migration/main/" class="">
      <i class='fa fa-cog'></i>
      <span>Upgrade Guide</span>
      <span class="menu-arrow fa fa-angle-right"></span>
  </a>
    <ul >
        
        
        
        
















    <li class="toctree-l2">
    <a href="/node-mongodb-native/3.2/upgrade-migration/upgrading/">
        <i class='fa fa-wrench'></i>
        Upgrading to 2.x
    </a>
  </li>


        
        
    </ul>
  </li>


          
            
            
















    <li class="toctree-l1">
    <a href="/node-mongodb-native/3.2/api">
        <i class='fa fa-file-text-o'></i>
        API Documentation
    </a>
  </li>


          
            
            
















    <li class="toctree-l1">
    <a href="https://github.com/mongodb/node-mongodb-native">
        <i class='fa fa-github'></i>
        Source Code
    </a>
  </li>


          
            
            
















    <li class="toctree-l1">
    <a href="/node-mongodb-native/3.2/issues-help/">
        <i class='fa fa-life-ring'></i>
        Issues &amp; Help
    </a>
  </li>


          
            
            
















    <li class="toctree-l1">
    <a href="https://gitter.im/mongodb/node-mongodb-native?utm_source=badge&amp;utm_medium=badge&amp;utm_campaign=pr-badge">
        <img src='https://badges.gitter.im/Join Chat.svg'/>
        
    </a>
  </li>


          
        </ul>
        
    </div>
  </div>
  
</aside>

<div class="option-popup closed hidden" id="optionsVersionsPopup">
<div class="option-header">
  <i class="fa fa-gear"></i>
  <span>OPTIONS</span>
  <i class="fa fa-angle-up pull-right"></i>
</div>
<div class="option-body">
  <ul>
      
      <li>
        <label>Version</label>
        <div class="btn-group btn-group-xs pull-right">
          <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
            Select Version <span class="caret"></span>
          </button>
          <ul class="dropdown-menu" role="menu" id="optionsVersionsMenu">
          </ul>
        </div>
      </li>

   


    

  </ul>
</div>
</div>



      
      <section id="main-content" class="content">
          <section class="main-column pull-left">
            <div class="document">
              <div class="documentwrapper">
                <div class="bodywrapper">
                  <div class="body">

                    <div class="alert alert-info" role="alert">
  Note: You are currently viewing version 3.2 of the Node.js driver documentation.
  <a href="https://www.mongodb.com/docs/drivers/node">Click here</a> for the latest version.
</div>



   
<a class="edit-link" href="https://github.com/mongodb/node-mongodb-native/blob/master/docs/reference/content/tutorials/collations.md" target="_blank" title="Edit tutorials/collations.md on GitHub"><i class="fa fa-pencil-square-o"></i></a>










<div class="bc">
<ul>
  
  
  
  
  
  <li><a href="/node-mongodb-native/3.2/tutorials/main/">Tutorials</a> <i class="fa fa-angle-right"></i></li>
  <li>Collations</li>
</ul>
</div>




<h1 id="overview">Overview</h1>

<p>Collations provide a set of rules which comply with the conventions
of a particular language when comparing strings.</p>

<p>For example, in Canadian French, the last accent in a given word
determines the sorting order.</p>

<p>Consider the following French words:</p>

<pre><code class="language-js">   cote &lt; coté &lt; côte &lt; côté
</code></pre>

<p>The sort order using the Canadian French collation would result in
the following:</p>

<pre><code class="language-js">   cote &lt; côte &lt; coté &lt; côté
</code></pre>

<p>If collation is unspecified, MongoDB uses the simple binary comparison for
strings.  As such, the sort order of the words would be:</p>

<pre><code class="language-js">cote &lt; coté &lt; côte &lt; côté
</code></pre>

<h1 id="usage">Usage</h1>

<p>You can specify a default collation for collections and indexes when
they are created, or specify a collation for CRUD operations and
aggregations. For operations that support collation, MongoDB uses the
collection&rsquo;s default collation unless the operation specifies a
different collation.</p>

<h2 id="collation-parameters">Collation Parameters</h2>

<pre><code class="language-xml">collation: {
   locale: &lt;string&gt;,
   caseLevel: &lt;bool&gt;,
   caseFirst: &lt;string&gt;,
   strength: &lt;int&gt;,
   numericOrdering: &lt;bool&gt;,
   alternate: &lt;string&gt;,
   maxVariable: &lt;string&gt;,
   backwards: &lt;bool&gt;
}
</code></pre>

<p>The only required parameter is <code>locale</code>, which the server parses as
an <a href="http://userguide.icu-project.org/locale">ICU format locale ID</a>.
For example, use <code>en_US</code> to represent US English and <code>fr_CA</code> to
represent Canadian French.</p>

<p>For a complete description of the available parameters, see the
<a href="https://www.mongodb.com/docs/manual/">MongoDB manual entry</a>`.</p>

<!-- NOTE placeholder link to Collation in the manual -->

<h2 id="assign-a-default-collation-to-a-collection">Assign a Default Collation to a Collection</h2>

<p>The following example creates a new collection
called <code>contacts</code> on the <code>test</code> database and assigns a default
collation with the <code>fr_CA</code> locale. Specifying a collation when you
create the collection ensures that all queries that are run against the
<code>contacts</code> collection use the <code>fr_CA</code> collation, unless the query
specifies another collation. Any indexes on the new collection also
inherit the default collation, unless the creation command specifies
another collation.</p>

<pre><code class="language-js">const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

// Create a new MongoClient
const client = new MongoClient(url);

// Use connect method to connect to the Server
client.connect(function(err, client) {
  assert.equal(null, err);
  console.log("Connected correctly to server");

  const db = client.db(dbName);

  createCollated(db, function() {
    client.close();
  });
});

function createCollated(db, callback) {
  db.createCollection('contacts',
    {
      'collation' :
        { 'locale': 'fr_CA' }
    },

    function(err, results) {
      console.log(&quot;Collection created.&quot;);
      callback();
    }
  );
};
</code></pre>

<h2 id="assign-a-default-collation-to-an-index">Assign a Default Collation to an Index</h2>

<p>To specify a collation for an index, use the <code>collation</code>
option when you create the index.</p>

<p>The following example creates an index on the <code>name</code>
field of the <code>contacts</code> collection, with the <code>unique</code> parameter
enabled and a default collation with <code>locale</code> set to <code>en_US</code>.</p>

<pre><code class="language-js">const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

// Create a new MongoClient
const client = new MongoClient(url);

// Use connect method to connect to the Server
client.connect(function(err, client) {
  assert.equal(null, err);
  console.log("Connected correctly to server");

  const db = client.db(dbName);

  createCollatedIndex(db, function() {
    client.close();
  });
});

function createCollatedIndex(db, callback) {
  // Get the contacts collection
  const collection = db.collection('contacts');
  // Create the index
  collection.createIndex(
    { 'name' : 1 },
    { 'unique' : 1 },
    { 'collation' : { 'locale' : 'en_US' } }, function(err, result) {
      console.log(result);
      callback(result);
  });
};
</code></pre>

<p>To use this index, make sure your queries also specify the same
collation. The following query uses the above index:</p>

<pre><code class="language-js">function findDocuments(db, callback) {
  const collection = db.collection( 'contacts' );
  collection.find({ 'city' : 'New York' }, { '_id' : 0 }, { 'collation' : {'locale' : 'en_US' }}).toArray(function(err, docs) {
      assert.equal(err, null);
      callback(docs);
  });
}
</code></pre>

<p>The following queries do <strong>NOT</strong> use the index. The first query uses no
collation, and the second uses a collation with a different <code>strength</code>
value than the collation on the index.</p>

<pre><code class="language-js">function findDocuments(db, callback) {
  const collection = db.collection( 'contacts' );
  collection.find({ 'city' : 'New York' }, { '_id' : 0 }).toArray(function(err, docs) {
      assert.equal(err, null);
      callback(docs);
  });
}

function findDocuments(db, callback) {
  const collection = db.collection( 'contacts' );
  collection.find({ 'city' : 'New York' }, { '_id' : 0 }, { 'collation' : { 'locale' : 'en_US' , 'strength' : 2 }}).toArray(function(err, docs) {
      assert.equal(err, null);
      callback(docs);
  });
}
</code></pre>

<h2 id="operations-that-support-collation">Operations that Support Collation</h2>

<p>All reading, updating, and deleting methods support collation. Some
examples are listed below.</p>

<h3 id="find-and-sort"><code>find()</code> and <code>sort()</code></h3>

<p>Individual queries can specify a collation to use when matching
and sorting results. The following query and sort operation uses
a German collation with the <code>locale</code> parameter set to <code>de</code>.</p>

<pre><code class="language-js">const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

// Create a new MongoClient
const client = new MongoClient(url);

// Use connect method to connect to the Server
client.connect(function(err, client) {
  assert.equal(null, err);
  console.log("Connected correctly to server");

  const db = client.db(dbName);

  findDocuments(db, function() {
    client.close();
  });
});

function findDocuments(db, callback) {
  const collection = db.collection( 'contacts' );
  collection.find({ 'city' : 'New York' }, { '_id' : 0 }, { 'collation' : {'locale' : 'de' } }).sort({ 'name': 1 }).toArray(function(err, docs) {
      assert.equal(err, null);
      console.log(&quot;Found the following records&quot;);
      console.log(docs)
      callback(docs);
  });
}
</code></pre>

<h3 id="findoneandupdate"><code>findOneAndUpdate()</code></h3>

<p>A collection called <code>names</code> contains the following documents:</p>

<pre><code class="language-js">{ &quot;_id&quot; : 1, &quot;first_name&quot; : &quot;Hans&quot; }
{ &quot;_id&quot; : 2, &quot;first_name&quot; : &quot;Gunter&quot; }
{ &quot;_id&quot; : 3, &quot;first_name&quot; : &quot;Günter&quot; }
{ &quot;_id&quot; : 4, &quot;first_name&quot; : &quot;Jürgen&quot; }
</code></pre>

<p>The following <code>findOneAndUpdate</code> operation on the collection
does not specify a collation.</p>

<pre><code class="language-js">const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

// Create a new MongoClient
const client = new MongoClient(url);

// Use connect method to connect to the Server
client.connect(function(err, client) {
  assert.equal(null, err);
  console.log("Connected correctly to server");

  const db = client.db(dbName);

  findAndUpdate(db, function() {
    client.close();
  });
});

function findAndUpdate(db, callback) {
  const collection = db.collection('names');
  collection.findOneAndUpdate({ first_name : { $lt: &quot;Gunter&quot; } }, { $set: { verified: true } }, function(err, result) {
    assert.equal(err, null);
    callback(result);
  });
}
</code></pre>

<p>Because <code>Gunter</code> is lexically first in the collection,
the above operation returns no results and updates no documents.</p>

<p>Consider the same <code>find_one_and_update</code> operation but with the
collation specified.  The locale is set to <code>de@collation=phonebook</code>.</p>

<div class="admonition note">
<h5 class="admonition-title">Note</h5>
<p>Some locales have a <code>collation=phonebook</code> option available for
use with languages which sort proper nouns differently from other
words. According to the <code>de@collation=phonebook</code> collation,
characters with umlauts come before the same characters without
umlauts.</p>

</div>


<pre><code class="language-js">const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

// Create a new MongoClient
const client = new MongoClient(url);

// Use connect method to connect to the Server
client.connect(function(err, client) {
  assert.equal(null, err);
  console.log("Connected correctly to server");

  const db = client.db(dbName);

  findAndUpdate(db, function() {
    client.close();
  });
});

function findAndUpdate(db, callback) {
  const collection = db.collection('names');
  collection.findOneAndUpdate({ first_name : { $lt: &quot;Gunter&quot; } }, { $set: { verified: true } }, {collation : { locale : 'de@collation=phonebook' } }, function(err, result) {
    assert.equal(err, null);
    console.log(result);
    callback(result);
  });
}
</code></pre>

<p>The operation returns the following updated document:</p>

<pre><code class="language-js">{ lastErrorObject: { updatedExisting: true, n: 1 },
  value: { _id: 3, first_name: 'Günter' },
  ok: 1 }
</code></pre>

<h3 id="findoneanddelete"><code>findOneAndDelete()</code></h3>

<p>Set the <code>numericOrdering</code> collation parameter to <code>true</code>
to sort numeric strings based on their numerical order instead of their
lexical order.</p>

<p>The collection <code>numbers</code> contains the following documents:</p>

<pre><code class="language-js">{ &quot;_id&quot; : 1, &quot;a&quot; : &quot;16&quot; }
{ &quot;_id&quot; : 2, &quot;a&quot; : &quot;84&quot; }
{ &quot;_id&quot; : 3, &quot;a&quot; : &quot;179&quot; }
</code></pre>

<p>The following example matches the first document in which field <code>a</code>
has a numeric value greater than 100 and deletes it.</p>

<pre><code class="language-js">const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

// Create a new MongoClient
const client = new MongoClient(url);

// Use connect method to connect to the Server
client.connect(function(err, client) {
  assert.equal(null, err);
  console.log("Connected correctly to server");

  const db = client.db(dbName);

  findAndDelete(db, function() {
    client.close();
  });
});

function findAndDelete(db, callback) {
  const collection = db.collection('numbers');
  collection.findOneAndDelete({ a : { $gt: &quot;100&quot; } }, {collation : { locale : 'en', numericOrdering: true } }, function(err, result) {
    assert.equal(err, null);
    console.log(result);
    callback(result);
  });
}
</code></pre>

<p>After the above operation, the following documents remain in the
collection:</p>

<pre><code class="language-js">{ &quot;_id&quot; : 1, &quot;a&quot; : &quot;16&quot; }
{ &quot;_id&quot; : 2, &quot;a&quot; : &quot;84&quot; }
</code></pre>

<p>If you perform the same operation without collation, the server deletes
the first document it finds in which the lexical value of <code>a</code> is
greater than <code>&quot;100&quot;</code>.</p>

<pre><code class="language-js">const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

// Create a new MongoClient
const client = new MongoClient(url);

// Use connect method to connect to the Server
client.connect(function(err, client) {
  assert.equal(null, err);
  console.log("Connected correctly to server");

  const db = client.db(dbName);

  findAndDelete(db, function() {
    client.close();
  });
});

function findAndDelete(db, callback) {
  const collection = db.collection('numbers');
  collection.findOneAndDelete({ a : { $gt: &quot;100&quot; } }, function(err, result) {
    assert.equal(err, null);
    console.log(result);
    callback(result);
  });
}
</code></pre>

<p>After the above operation, the following documents remain in the
collection:</p>

<pre><code class="language-js">{ &quot;_id&quot; : 2, &quot;a&quot; : &quot;84&quot; }
{ &quot;_id&quot; : 3, &quot;a&quot; : &quot;179&quot; }
</code></pre>

<h2 id="aggregation">Aggregation</h2>

<p>To use collation with an aggregation operation, add the collation
document after the array of pipeline stages.</p>

<p>The following aggregation example uses a collection called <code>names</code>
and groups the <code>first_name</code> field together, counts the total
number of results in each group, and sorts the
results by German phonebook order.</p>

<pre><code class="language-js">const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

// Create a new MongoClient
const client = new MongoClient(url);

// Use connect method to connect to the Server
client.connect(function(err, client) {
  assert.equal(null, err);
  console.log("Connected correctly to server");

  const db = client.db(dbName);

  countNames(db, function() {
    client.close();
  });
});

function countNames(db, callback) {
  const collection = db.collection( 'names' );
  collection.aggregate( 
      [ 
        { '$group': { '_id': &quot;$first_name&quot;, 'nameCount': { '$sum': 1 } } },
        { '$sort' : { '_id' : 1 } }
      ], { collation : { locale : 'de@collation=phonebook' } },

      function(err, docs) {
        assert.equal(err, null);
        console.log(&quot;Found the following records&quot;);
        console.log(docs)
        callback(docs);
      }
  );
}
</code></pre>






<div id="btnv">
  
  <div class="pull-left">
  <a class="navigation prev" href="/node-mongodb-native/3.2/tutorials/connect/">
      <i class="fa fa-long-arrow-left"> </i> Connect to MongoDB
  </a>
  </div>
  
  
  <div class="pull-right">
  <a class="navigation next" href="/node-mongodb-native/3.2/tutorials/collections/">
    Collections <i class="fa fa-long-arrow-right"> </i>
  </a>
  
</div>
</div>


</div>
<div class="right-column">
<div class="wrapper">
  
  <div class="toc">
    <span class="toc-header">On this page</span>
    <nav id="TableOfContents">
<ul>
<li><a href="#overview">Overview</a></li>
<li><a href="#usage">Usage</a>
<ul>
<li><a href="#collation-parameters">Collation Parameters</a></li>
<li><a href="#assign-a-default-collation-to-a-collection">Assign a Default Collation to a Collection</a></li>
<li><a href="#assign-a-default-collation-to-an-index">Assign a Default Collation to an Index</a></li>
<li><a href="#operations-that-support-collation">Operations that Support Collation</a>
<ul>
<li><a href="#find-and-sort"><code>find()</code> and <code>sort()</code></a></li>
<li><a href="#findoneandupdate"><code>findOneAndUpdate()</code></a></li>
<li><a href="#findoneanddelete"><code>findOneAndDelete()</code></a></li>
</ul></li>
<li><a href="#aggregation">Aggregation</a></li>
</ul></li>
</ul>
</nav>
  </div>
  
</div>
</div>

</div>
</div>
</div>
</section>
</section>

</section>


<script type="text/javascript">
  var DOCUMENTATION_OPTIONS = {
   URL_ROOT:    "/node-mongodb-native/3.2",
   VERSION:     "3.2",
   COLLAPSE_INDEX: false,
   FILE_SUFFIX: '.html',
   HAS_SOURCE:  true
  };
</script>
<script type="text/javascript" src="/node-mongodb-native/3.2/js/jquery.js"></script>
<script type="text/javascript" src="/node-mongodb-native/3.2/lib/bootstrap.js"></script>
<script type="text/javascript" src="/node-mongodb-native/3.2/js/navbar.js"></script>
<script type="text/javascript" src="/node-mongodb-native/3.2/lib/highlight/highlight.pack.js"></script>
<script type="text/javascript" src="/node-mongodb-native/3.2/js/scripts.js"></script>
<script type="text/javascript" src="/node-mongodb-native/3.2/lib/bootstrap-toggle/bootstrap-toggle.min.js"></script>
<script type="text/javascript" src="/node-mongodb-native/3.2/js/java.js"></script>
<script type="text/javascript" src="/node-mongodb-native/3.2/js/toggle-switch.js"></script>

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-29229787-1', 'auto');
  ga('send', 'pageview');
</script>
</body>
</html>

